package com.wolf.cx330.main.act

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.View
import com.wolf.cx330.R
import com.wolf.cx330.base.BaseActivity

class SplashAct : BaseActivity() {


    private lateinit var va: ObjectAnimator

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        checkPermission {
            startAnimation()
        }
    }


    private fun startAnimation() {
        val eye = findViewById<View>(R.id.eye)
        eye.alpha = 0.2f
        val black = findViewById<View>(R.id.black_view)
        val white = findViewById<View>(R.id.white_view)
        va = ObjectAnimator.ofFloat(eye, "alpha", 0.2f, 1f)
        va.duration = 1000
        va.repeatCount = ValueAnimator.INFINITE
        va.repeatMode = ValueAnimator.REVERSE
        va.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}
            override fun onAnimationEnd(animation: Animator) {}
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {
                va.cancel()
                val va1 = ObjectAnimator.ofFloat(white, "alpha", 0f, 1f)
                val va2 = ObjectAnimator.ofFloat(black, "alpha", 1f, 0f)
                val animatorSet = AnimatorSet()
                animatorSet.duration = 1500
                animatorSet.addListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        black.postDelayed({
                            MainAct.actionStart(this@SplashAct)
                            finish()
                        }, 500)
                    }

                    override fun onAnimationCancel(animation: Animator) {}
                    override fun onAnimationRepeat(animation: Animator) {}
                })
                animatorSet.playTogether(va1, va2)
                animatorSet.start()
            }
        })
        va.start()
    }

    override fun onBackPressed() {
    }

}